package com.ljkj.cordial.chat.presenter.response;

import android.app.Activity;
import android.text.TextUtils;

import com.lanjiu.lib.imclient.ims.IMSUtils;
import com.lanjiu.pro.business.BusinessProtocolEntities;
import com.lanjiu.pro.business.BusinessProtocolMessages;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.EmptyMessageEvent;
import com.ljkj.cordial.chat.bean.MsgType;
import com.ljkj.cordial.chat.dao.ChatTableBean;
import com.ljkj.cordial.chat.dao.DbController;
import com.nic.view.util.JLog;

import java.util.List;


public class DelFriendOffMessageProtocolResponse extends AResponseBusinessPresenter {
    @Override
    public void onResponse(Activity activity, boolean isOnScreen, String msgType, Object msg) {
        BusinessProtocolMessages.DelFriendOffMessageProtocol response = (BusinessProtocolMessages.DelFriendOffMessageProtocol) msg;
        BusinessProtocolEntities.ChatMessage chatMessage = response.getChatMessage();
        BusinessProtocolEntities.RegisteredFriend regFriend = response.getRegisteredFriend();
        BusinessProtocolEntities.TouristsFriend tourFriend = response.getTouristsFriend();
        BusinessProtocolEntities.RegisteredUser regUser = response.getRegisteredUser();
        BusinessProtocolEntities.TouristsUser tourUser = response.getTouristsUser();
        String targetId = null;
        String targetType = null;
        String targetName = null;
        if (response.hasRegisteredFriend() && !ChatApp.getInstance().getUserId().equals(regFriend.getFriendUserId())) {
            targetId = regFriend.getFriendUserId();
            targetType = IMSUtils.userTypeAnalysis(regFriend.getFriendUserId());
            targetName = regFriend.getFriendNickname();
        }
        if (response.hasTouristsFriend() && !ChatApp.getInstance().getUserId().equals(tourFriend.getFriendUserId())) {
            targetId = tourFriend.getFriendUserId();
            targetType = IMSUtils.userTypeAnalysis(tourFriend.getFriendUserId());
            targetName = tourFriend.getFriendNickname();
        }
        if (response.hasRegisteredUser() && !ChatApp.getInstance().getUserId().equals(regUser.getUserId())) {
            targetId = regUser.getUserId();
            targetType = IMSUtils.userTypeAnalysis(targetId);
            targetName = regUser.getNickname();
        }
        if (response.hasTouristsUser() && !ChatApp.getInstance().getUserId().equals(tourUser.getTemporaryId())) {
            targetId = tourUser.getTemporaryId();
            targetType = IMSUtils.userTypeAnalysis(targetId);
            targetName = tourUser.getNickname();
        }
        if (response.hasChatMessage()) {
            String msgIds = chatMessage.getMsgId();
            if (TextUtils.isEmpty(msgIds)) {
                return;
            }
            for (String msgId : msgIds.split(",")) {
                ChatTableBean u = ChatTableBean.queryByMessageId(msgId);
                int chat = Canstants.SINGLE_CHAT;
                if (u != null) {
                    JLog.e(msgId);
                    u.setChatType(MsgType.Del_Chat_Msg);
                    u.setStartReadFinish(false);
                    if (u.getRelateId() != null && u.getRelateId().longValue() > 0) {
                        ChatTableBean ut = ChatTableBean.getTableFromId(String.valueOf(u.getRelateId()));
                        ut.setChatType(MsgType.Del_Chat_Msg);
                        DbController.getInstance().getWrite().getChatTableBeanDao().update(ut);
                    }
                    DbController.getInstance().getWrite().getChatTableBeanDao().update(u);
                    postEvent(new EmptyMessageEvent(chat, targetId, targetName, targetType, msgId, EmptyMessageEvent.REMOVE));
                    refresh(activity);
                }
            }
        }
    }

}
